#include<stdio.h>
#include<string.h>
#include<errno.h>
#include<stdlib.h>
//#include<unistd.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
//函数指针类型
typedef void(*func_t)();
#define NUM 5
func_t handlers[NUM+1];

//如下是任务
void DownLoad()
{
    printf("我是一个下载任务........\n");
}
void Flush()
    {
        printf("i am Flush tasks........\n");                                                                                                                                                                  
   }  
void Log()
   {
    printf("i am Record logs tasks........\n");                                                                                                                                                                  
    }  
//注册
void registerHanler(func_t h[],func_t f)
{
int i = 0;
for(;i<NUM;i++)
{
    if(h[i]==NULL)break;
    
}
    if(i == NUM) return ;
    h[i]= f;
    h[i+1] = NULL;
}
int main()
{
    registerHanler(handlers,DownLoad);
    registerHanler(handlers,Flush);
    registerHanler(handlers,Log);

    pid_t id = fork();
    if(id==0)
    {
        //子进程
        int cnt = 3;
        while(cnt)
        {
            printf("我是一个子进程，pid:%d,ppid:%d\n",getpid(),getppid());
            sleep(1);
            cnt--;
        }
        exit(1);
    }
    
    
    //父进程
//    pid_t rid = wait(NULL);
// pid_t rid = waitpid(id,NULL,0);
//    int status = 0;
//    pid_t rid = waitpid(id,&status,0);
//    if(rid>0)
//    {
//
//       // printf("wait success,rid:%d,exit code:%d,exit sigal:%d\n",rid,(status>>8)&0xFF,(status&0x7F) );//rid
//        if(WIFEXITED(status))
//        printf("wait success,rid:%d,exit code:%d,exit sigal:%d\n",rid,WEXITSTATUS(status),(status&0x7F) );//rid  
//        else 
//            printf("子进程退出异常！\n");
////
////fclose(fp);
////    return 0;
//
////
////fclose(fp);
////    return 0;
//    }
//    else{
//
//        printf("wait failed:%d:%s\n",errno,strerror(errno));
//    }

//父进程
 while(1)
 {
     int status = 0;
     pid_t rid  = waitpid(id,status,WNOHANG);
     if(rid>0)
     {
          printf("wait success,rid:%d,exit code:%d,exit sigal:%d\n",rid,(status>>8)&0xFF,(status&0x7F) );//rid
       break;
     }
     else if(rid == 0)
     {
         //函数指针进行回调处理
         int i = 0;
         for(;handlers[i];i++)
         {
             handlers[i]();
         }
         printf("本轮调用结束，子进程没有退出\n");
         sleep(1);
     }
     else 
     {
         printf("等待失败\n");
         break;
     }
 }

    return 0;
}
//void fun()
//{
//printf("fun begin\n");
//_exit(4);
//printf("fun end\n");
//}
//int main()
//{
////    fun();
//    printf("main");
//    sleep(2);
//   _exit(23);
////   exit(23); 
//    //int  a = 10;
//    //a /= 0;
//   // return 100;
////    int i = 0 ;
////    for( ;i<200 ;i++)
////    {
////        printf("%d->%s\n", i,strerror(i));
////    }
////    return 0;
//////printf( "hello world!\n");
////FILE* fp = fopen("log.txt","r");
////if (fp=NULL) return 13;
////fclose(fp);
//return  0;
//}
//
//
